<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<div id="signCardSingleManage_toolbar" style="height:100px">

    <div id="signCardSingleManage_panel" class="easyui-panel"
         style="width:1650px;height:100%;padding:10px;background:#fafafa;">
        <form id="signCardSingleManage_form">
            <div style="float: left">
                <div>
                    <input id="signCardSingleManage_form_pkHrorg" name="pkOrg"
                           data-options="valueField:'pkhrorg',textField:'name',label:'组织',labelWidth:'40',required :'required',prompt:'查询必选项'"
                           style="width:260px;"/>
                    <input id="signCardSingleManage_form_Signdate" name="beginSignDate"
                           data-options="label:'申请日期',labelWidth:'60',required :'required',prompt:'查询必选项'"
                           style="width:240px" class="easyui-datebox"/>
                    —
                    <input id="signCardSingleManage_form_Signdate2" name="endSignDate" style="width:180px"
                           class="easyui-datebox" data-options="required:'required',prompt:'查询必选项'">
                    <a style="color:red" href="#" title="请选择开始日期和结束日期！" id="tooltipMessage"></a>
                </div>
                <div style="margin-top: 8px">
                    <input id="signCardSingleManage_form_pkDeptV" name="pkDepts" class="easyui-combotree"
                           data-options="label:'部门',labelWidth:'40',multiple:true,prompt:'需先选择组织'"/>

                    <!--根据部门选择员工 <input style="width: 170px" id="signCardSingleManage_form_pkPsndoc" class="easyui-combobox" data-options="label:'员工姓名',labelWidth:'60',valueField:'id',textField:'text'" name="pkPsndoc" value="请先选择部门">  -->
                    <input class="easyui-textbox" name="pkPsndoc"
                           data-options="label:'员工姓名',labelWidth:'60',prompt:'请输入姓或全名'">
                    <input class="easyui-textbox" name="psndocCode"
                           data-options="label:'工号',labelWidth:'40',prompt:'请输入大概工号'">
                    <input id="signCardSingleManage_form_billCode" class="easyui-textbox" name="billCode"
                           data-options="label:'签卡单号',labelWidth:'60',prompt:'请输入正确的单号',required :'required'">
                </div>
            </div>
            <div style="margin-top: 8px;float: left;">
                <a id="signCardSingleManage_form_btn" href="#" style="width:120px;height: 50px;margin-left: 10px"
                   class="easyui-linkbutton" data-options="iconCls:'icon-search'">查询</a>
            </div>
        </form>
    </div>
    <div id="signCardSingleManage_table_pagination" style="background:#efefef;border:1px solid #ccc;"></div>
</div>

<table id="signCardSingleManage_table" class="easyui-datagrid" data-options="fitColumns:true">
    <thead>
    <tr>
        <th data-options="field:'pkSignh',width:100,checkbox:'true'">主键</th>
        <th data-options="field:'pkSignreg',width:100,hidden:'true'">登记表主键</th>
        <th data-options="field:'billCode',width:100">申请单编码</th>
        <th data-options="field:'approveNote',width:50">审批状态</th>
        <th data-options="field:'applyDate',width:100,align:'center'">申请日期</th>
        <th data-options="field:'psndocName',width:100,align:'center'">员工姓名</th>
        <th data-options="field:'psndocCode',width:100,align:'center'">员工工号</th>
        <th data-options="field:'hrorgName',width:250,align:'center'">组织名</th>
        <th data-options="field:'deptName',width:200,align:'center'">部门</th>
    </tr>
    </thead>
</table>

<!-- 签卡详细窗口 -->
<div id="signCardSingleManage_item" data-options="buttons:'#signCardSingleManage_item_btn',">
    <table id="signCardSingleManage_item_table" style="width:600px;height:300px"
           data-options="fitColumns:true,buttons:'#signCardSingleManage_item_btn',fitColumns:'true',fit:'true'">
        <thead>
        <tr>
            <th data-options="field:'total',width:100,hidden:'true'">总数</th>
            <th data-options="field:'pkSignb',width:100,checkbox:'true'">主键</th>
            <th data-options="field:'signtime',width:230,editor:{type:'datetimebox',options:{}}">签卡时间</th>
            <th data-options="field:'name',width:250,editor:{type:'combobox',
		            options:{url:'/signCardSingle/getBdDefdoc', valueField:'pkDefdoc', textField:'name',limitToList:'true',editable:false   
		            }}">签卡原因
            </th>
            <th data-options="field:'signremark',width:250">签卡说明</th>
        </tr>
        </thead>
    </table>
</div>
<!-- 签卡详细窗口底部按钮 -->
<div id="signCardSingleManage_item_btn">
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onClick="deleteItem()">删除</a>
    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onClick="updateItem()">修改</a>
</div>
<script type="text/javascript">
    $(function () {
        var sFormBeginSigndate = $('#signCardSingleManage_form_Signdate');
        var sFormEndSigndate = $('#signCardSingleManage_form_Signdate2');
        var sFormBillCode = $('#signCardSingleManage_form_billCode');
        var sCSMTablePagination = $("#signCardSingleManage_table_pagination");
        sCSMTablePagination.hide();

        $('#signCardSingleManage_form_btn').linkbutton({
            onClick: function () {
                submitFrom();
            }
        })

        sCSMTablePagination.pagination({
            onSelectPage: function (pageNumber, pageSize) {
                submitFrom(pageNumber, pageSize);
            }
        })

        //查询表格数据
        function submitFrom(pageNumber, pageSize) {
            $('#signCardSingleManage_form').form('submit', {
                method: 'post',
                novalidate: true,
                url: '/signCardSingle/conditionSelectTbmSignh',
                onSubmit: function (param) {
                    var isValid = $(this).form('validate');
                    //console.log(pageNumber,pageSize);  ie浏览器不支持 console.log
                    console.log(param)
                    if (pageNumber) {
                        param.page = pageNumber;
                        param.rows = pageSize;
                    } else {
                        param.page = 1;
                        param.rows = 10;
                    }

                    return (signCardSingleManageFormValidate(isValid));
                }, success: function (data) {
                    signCardSingleManageFormSuccess(data);
                }
            });
        }

        //删除主表数据
        $("#signCardSingleManage_table").datagrid({
            fit: true,
            toolbar: [{
                text: '删除签卡单',
                iconCls: 'icon-remove',
                plain: false,
                handler: function () {
                    var rows = $("#signCardSingleManage_table").datagrid('getSelections');
                    //console.log(rows);
                    var signhSize = rows.length;
                    var pkSinghs = [];
                    for (var i = 0; i < signhSize; i++) {
                        pkSinghs.push(rows[i].pkSignh);
                    }
                    $.messager.confirm('删除警告', '您确认想要删除这<span style="color:red;">' + signhSize + '</span>条签卡单吗？', function (rr) {
                        if (rr) {
                            if (signhSize > 0) {
                                signCardSingleManageTablePost(rows, pkSinghs);
                            }
                        }
                    })
                }
            }]
        })

        function signCardSingleManageTablePost(rows, pkSinghs) {
            $.post({
                url: '/signCardSingle/deleteTbmSignh',
                contentType: 'application/json',
                data: JSON.stringify(rows),
                success: function (rmTbmSignhData) {
                    if (rmTbmSignhData.statu == 200) {
                        //删除成功,清除选中的行
                        for (var i = 0; i < rows.length; i++) {
                            var num = $("#signCardSingleManage_table").datagrid('getRowIndex', pkSinghs[i]);
                            //console.log("主表要移除的数据编号",num);
                            $("#signCardSingleManage_table").datagrid('deleteRow', num);
                        }
                    }
                    windowsOperation(null, null, rmTbmSignhData);
                }
            })
        }

        //表格数据验证逻辑
        function signCardSingleManageFormValidate(isValid) {
            if (!isValid) {
                if (!sFormBeginSigndate.textbox('isValid')
                    || !sFormEndSigndate.textbox('isValid')) {
                    $("#tooltipMessage").tooltip('show', "");
                }
            } else {
                //按钮禁用
                $("#signCardSingleManage_form_btn").linkbutton("disable");
            }
            return isValid;	// 返回false终止表单提交
        }

        //表格成功获取数据
        function signCardSingleManageFormSuccess(data) {
            var data = eval('(' + data + ')');
            //分页
            sCSMTablePagination.pagination({
                total: data.total
            })
            sCSMTablePagination.show();
            //查询按钮开启
            $("#signCardSingleManage_form_btn").linkbutton("enable");
            if (data.statu == 200) {
                $("#signCardSingleManage_table").datagrid({
                    striped: true,
                    rownumbers: true,
                    selectOnCheck: true,
                    idField: 'pkSignh',
                    data: data,
                    toolbar: '#signCardSingleManage_toolbar',
                    fit: true,
                    onDblClickRow: function (index, row) {
                        signCardSingleManageFormBtnOnDblClickRow(index, row);
                    }
                })
            } else if (data.statu == 404) {
                warnSessionConfirm();

            } else {
                topCommonMessage(data.message);
                $('#signCardSingleManage_table').datagrid('loadData', data);
            }
        }

        //双击事件打开签卡详细窗口
        function signCardSingleManageFormBtnOnDblClickRow(index, row) {
            //获取表tbm_signh的id：
            var pkSignh = row.pkSignh;
            var psndocName = row.psndocName;
            if (pkSignh != null) {
                $('#signCardSingleManage_item').dialog({
                    title: '签卡详情[' + psndocName + '] <span style="color:red">【提示】：1、先双击目标单元格进行更改数据，2、勾选修改的行最后点击修改按钮</span>',
                    width: 650,
                    height: 390,
                    closed: false,
                    cache: false,
                    modal: true,
                    pagination: true,
                    pagePosition: 'both',
                    onOpen: function () {
                        signCardSingleManageItemOpen(row);
                    }
                });
            }
        }

        //打开签卡单详情触发事件
        function signCardSingleManageItemOpen(row) {
            var pkSignbData = dataTbmSingbResult(row);
            if (pkSignbData.statu == 200) {
                $("#signCardSingleManage_item_table").datagrid({
                    onAfterEdit: function (index, itemRow, changes) { //用户完成编辑触发事件
                        $.post({
                            url: '/signCardSingle/updateTbmSignbByPkSignb',
                            contentType: 'application/json',
                            data: JSON.stringify(itemRow),
                            success: function (data) {
                                if (data.statu == 404) {
                                    warnSessionConfirm();
                                }
                                pkSignbData = dataTbmSingbResult(row);
                                $("#signCardSingleManage_item_table").datagrid('loadData', pkSignbData);
                                windowsOperation(null, "signCardSingleManage_item_table", data);
                            }
                        })
                    },
                    onDblClickCell: function (index, field, value) { //行号，属性名，值   双击一个单元格
                        $("#signCardSingleManage_item_table").datagrid("beginEdit", index);
                    },
                    state: 'closed',
                    data: pkSignbData
                })
            }
        }

        //根据签卡主表查询签卡但子表详情
        function dataTbmSingbResult(row) {
            var result;
            $.post({
                url: '/signCardSingle/getTbmSignbByPkSignh',
                async: false,
                contentType: 'application/json',
                data: JSON.stringify(row),
                success: function (pkSignbData) {
                    if (pkSignbData.statu == 404) {
                        $.messager.confirm('系统提示', '登录已过期，要为您转回登录界面吗?', function (c) {
                            if (c) {
                                northLogout();
                            }
                        });
                    }
                    //console.log("根据主表主键查询的子表详情：",pkSignbData);
                    result = pkSignbData;
                }
            });
            return result;
        }

        //根据组织查询部门
        $("#signCardSingleManage_form_pkHrorg").combobox({
            url: '/user/getUserOrgHrorg',
            onChange: function (newValue, oldValue) {
                $("#signCardSingleManage_form_pkDeptV").combotree({
                    url: '/signCardSingle/selectOrgDeptByPkHrorg?pkHrorg=' + newValue
                })
            }
        })

        //日期处理
        sFormBeginSigndate.datebox({
            editable: false,
            onSelect: function (date) {
                $("#tooltipMessage").tooltip('hide', "");
                //判断选择了日期时间进行释放签卡单编码的验证
                if (sFormEndSigndate.datebox('getText')) {
                    sFormBillCode.textbox("disableValidation");
                }
                var currentDate = new Date();
                var month = date.getMonth();
                var currentMonth = currentDate.getMonth();
                var currentYear = currentDate.getFullYear();
                var year = date.getFullYear();
                if (year != currentYear || month != currentMonth) {
                    //alert("签卡日期范围不允许跨月");
                    //console.log($("#signCardSingleManage_form_Signdate").next("span").children("input[type='hidden']"));
                    //$("#signCardSingleManage_form_Signdate").next("span").children("input")[1].value="";
                    //$(date).datebox('setValue', '');
                }
            }
        })
        sFormEndSigndate.datebox({
            editable: false,
            onSelect: function (date) { //选择了时间后触发事件
                //隐藏提示消息
                //判断选择了日期时间进行释放签卡单编码的验证
                if (sFormBeginSigndate.datebox('getText')) {
                    $("#tooltipMessage").tooltip('hide', "");
                    sFormBillCode.textbox("disableValidation");
                }
                var currentDate = new Date();
                var month = date.getMonth();
                var currentMonth = currentDate.getMonth();
                var currentYear = currentDate.getFullYear();
                var year = date.getFullYear();
                if (year != currentYear || month != currentMonth) {
                    //alert("签卡日期范围不允许跨月");
                    //$(date).datebox('setValue', '');
                }
            }
        })
        //要求输入了签卡单，不用再输入日期
        sFormBillCode.textbox({
            onChange: function (newValue, oldValue) {
                if (newValue) {
                    $("#tooltipMessage").tooltip('hide', "");
                    sFormBeginSigndate.datebox("disableValidation");
                    sFormEndSigndate.datebox("disableValidation");
                } else {
                    sFormBeginSigndate.datebox("enableValidation");
                    sFormEndSigndate.datebox("enableValidation");
                }
            }
        })
    })

    //日期提示语
    $("#tooltipMessage").tooltip({
        position: 'top',
        deltaX: 520,
        deltaY: 10,
        showEvent: 'validate',
        hideDelay: 100
    });

    //修改操作
    function updateItem() {
        //console.log("修改数据");
        var hasSelects = $("#signCardSingleManage_item_table").datagrid("getSelections");
        var nums = hasSelects.length;
        if (nums > 0) {
            for (var i = 0; i < nums; i++) {
                var row = hasSelects[i];
                rowNumber = $("#signCardSingleManage_item_table").datagrid("getRowIndex", row);

                $("#signCardSingleManage_item_table").datagrid("endEdit", rowNumber);

            }
        } else {
            topCommonMessage("请勾选要修改或删除的数据！！");
        }
    }


    //删除子表数据
    function deleteItem() {
        var tbmSignbExtends = $('#signCardSingleManage_item_table').datagrid('getSelections');
        var size = tbmSignbExtends.length;
        //console.log("删除子表");
        $.messager.confirm('删除警告', '您确认想要删除这<span style="color:red;">' + size + '</span>条签卡单明细吗？', function (rr) {
            if (rr) {
                if (size > 0) {
                    deleteItemPost(tbmSignbExtends, size);
                }
            }
        })
    }

    function deleteItemPost(tbmSignbExtends, size) {
        //console.log("ceshi ");
        $.post({
            url: '/signCardSingle/deleteTbmSignb',
            contentType: 'application/json',
            data: JSON.stringify(tbmSignbExtends),
            success: function (data) {
                //返回状态200是删除所有的子表数据，201是删除部分
                if (data.statu == 200) {
                    //关闭窗口
                    $('#signCardSingleManage_item').dialog('close');

                    //	var row=$("#signCardSingleManage_table").datagrid('getSelected');
                    var num = $("#signCardSingleManage_table").datagrid('getRowIndex', data.pkSignh);
                    $("#signCardSingleManage_table").datagrid('deleteRow', num);

                } else if (data.statu == 201) {
                    for (var i = 0; i < size; i++) {
                        var itemNum = $("#signCardSingleManage_item_table").datagrid('getRowIndex', tbmSignbExtends[i]);
                        $("#signCardSingleManage_item_table").datagrid('deleteRow', itemNum);
                    }
                }
                windowsOperation(null, "signCardSingleManage_item_table", data);
            }
        })
    }

    //根据部门查询人员
    /*
    $("#signCardSingleManage_form_pkDeptV").combotree({
        //查询部门人员
        onCheck:function(node){
            var t = $('#signCardSingleManage_form_pkDeptV').combotree('tree');
            var nodes = t.tree('getChecked');
            console.log(nodes);
            console.log(nodes.children);//undefined 和 数组
            //console.log(nodes[0].id);//
            var pkDepts=[];
            for(var i=0;i<nodes.length;i++){
                pkDepts.push(nodes[i].id);
            }
            if(pkDepts.length>0){
                //根据后台获取数据
                $.post({
                    url:'/signCardSingle/getDeptPsndoc',
                    data:{'pkDepts':pkDepts},
                    success:function(data){
                        $('#signCardSingleManage_form_pkPsndoc').combobox({
                            data:data,
                            singleSelect:true,
                            valueField:'pkPsndoc',
                            textField:'name'
                        });
                    }
                })
            }else{
                $('#signCardSingleManage_form_pkPsndoc').combobox({
                    data:[],
                    valueField:'pkPsndoc',
                    textField:'name'
                });
            }
        }
    })
    */
</script>
</body>
</html>